package net.lab1024.sa.admin.module.business.erp.poo.service;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.erp.inv.domain.vo.TargetVo;
import net.lab1024.sa.admin.module.business.erp.poo.dao.StoordaDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.StoordabackDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.StoordbackDao;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.StoordabackEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.StoordbackEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.StoordbackAddForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.StoordbackQueryForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.StoordbackUpdateForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.StoordabackVO;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.StoordbackVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 采购入库单 Service
 *
 * @Author AKITA
 * @Date 2025-08-04 14:52:58
 * @Copyright AKITA
 */

@Service
public class StoordbackService {

    @Resource
    private StoordbackDao stoordbackDao;
    @Resource
    private StoordaDao stoordaDao;
    @Resource
    private StoordabackDao stoordabackDao;


    public PageResult<StoordbackVO> queryPage(StoordbackQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<StoordbackVO> list = stoordbackDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }


    @Transactional
    public ResponseDTO<Long> add(StoordbackAddForm addForm) {
        StoordbackEntity stoordbackEntity = SmartBeanUtil.copy(addForm, StoordbackEntity.class);
        stoordbackDao.insert(stoordbackEntity);
        List<StoordabackEntity> lista = addForm.getLista();
        for (StoordabackEntity stoordabackEntity : lista) {
            stoordabackEntity.setId(stoordbackEntity.getId());
            stoordabackDao.insert(stoordabackEntity);
        }



        return ResponseDTO.ok(stoordbackEntity.getId());
    }


    public ResponseDTO<String> update(StoordbackUpdateForm updateForm) {
        StoordbackEntity stoordbackEntity = SmartBeanUtil.copy(updateForm, StoordbackEntity.class);
        stoordbackDao.updateById(stoordbackEntity);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        stoordbackDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> delete(Long id) {
        stoordbackDao.deleteById(id);
        stoordabackDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<StoordbackVO> info(Long id) {
        StoordbackEntity stoordbackEntity = stoordbackDao.selectById(id);
        StoordbackVO copy = SmartBeanUtil.copy(stoordbackEntity, StoordbackVO.class);
        List<StoordabackEntity> stoordabackEntities = stoordabackDao.selectList(new LambdaQueryWrapper<StoordabackEntity>().eq(StoordabackEntity::getId, id));
        List<StoordabackVO> stoordabackVOS = SmartBeanUtil.copyList(stoordabackEntities, StoordabackVO.class);
        ArrayList<TargetVo> targetVob = new ArrayList<>();
        for (StoordabackEntity item : stoordabackEntities) {
            if (item.getBaseId() != null) {
                TargetVo targetVo = new TargetVo();
                targetVo.setId(item.getBaseId());
                targetVo.setLineNum(item.getBaseLineNum());
                targetVo.setItemId(item.getItemId());
                targetVo.setItemName(item.getItemName());
                targetVo.setQty(item.getQty());
                targetVob.add(targetVo);
            }
        }

//        List<SubordaEntity> subordaEntities = subordaDao.selectList(new LambdaQueryWrapper<SubordaEntity>().eq(SubordaEntity::getBaseId, id));


//
//
        ArrayList<TargetVo> targetVoc = new ArrayList<>();
//        for (SubordaEntity item : subordaEntities) {
//            TargetVo targetVo = new TargetVo();
//            targetVo.setId(item.getId());
//            targetVo.setLineNum(item.getLineNum());
//            targetVo.setItemId(item.getItemId());
//            targetVo.setItemName(item.getItemName());
//            targetVo.setQty(item.getQty());
//            targetVoc.add(targetVo);
//        }

        copy.setLista(stoordabackVOS);
        copy.setListb(targetVob);
        copy.setListc(targetVoc);

        return ResponseDTO.ok(copy);




    }

    public ResponseDTO<StoordbackVO> infoUnexecute(Long id) {
        StoordbackEntity stoordbackEntity = stoordbackDao.selectById(id);
        StoordbackVO copy = SmartBeanUtil.copy(stoordbackEntity, StoordbackVO.class);
        List<StoordabackVO> puordaVOS = stoordabackDao.infoUnexecute();
        copy.setLista(puordaVOS);
        return ResponseDTO.ok(copy);
    }
}
